// pages/my-account/index.js
// import { getProductHot } from '../../api/store.js';
import { openRechargeSubscribe } from '../../utils/SubscribeMessage.js';
import { getUserInfo, userActivity, zhifu } from '../../api/user.js';
import { getCouponsOrderPrice} from '../../api/order.js';

const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '我的账户',
      'color': false,
    },
    userInfo:{},
    isClose:false,
    recharge_switch:0,
    coupon: { coupon: false, list: [], statusTile:'立即使用'},//优惠券组件
    couponId:0,//优惠券id
    couponTitle:'请选择',//优惠券
    coupon_price:0,//优惠券抵扣金额
    active:0,
    number:'',
  },

  /**
   * 登录回调
  */
  onLoadFun:function(){
    this.getUserInfo();
    this.get_activity();
    this.getCouponList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bindKeyInput: function (e) {
    this.setData({
      number: e.detail.value
    })
  },
    /*
  * 付款
  */
  // 校验支付表单
  verifyForm: function(pay, couponId) {
    let that = this;
    if (parseFloat(pay) <= 0 || !pay) {
      return app.Tips({ title: '请填写付款金额' });
    } else if (Number(pay) > Number(that.data.userInfo.now_money)) {
      console.log('now_money',that.data.userInfo.now_money)
      wx.showModal({
        title: '余额不足',
        showCancel: false,
        confirmText: '去充值',
        success(res){
          if (res.confirm){
            wx.navigateTo({url:'/pages/user_payment/index'});
          } else if (res.cancel){
            return app.Tips({title:'已取消'});
          }
        }
      })
    } else {
        //支付//计算金额
        zhifu(pay,couponId, 'calc' ).then(function (res) {
          // app.Tips({ title: '获取成功' });
          let point = res.data.point;
          let realpay = res.data.realpay;
          console.log(res.data.point,res.data.realpay);
          let contentText = '本次支付将扣除'+realpay+'元，将会获得'+point+'积分';
          wx.showModal({
            content: contentText,
            cancelText: '考虑一下',
            confirmText: '确认付款',
            success(res){
              if (res.confirm){
                // 调用付款接口
                that.comfirmPay(pay, couponId, point);
              } else if (res.cancel){
                return app.Tips({title:'已取消'});
              }
            },
          })
        },function(res){
          return app.Tips({title:res.msg});
        });
    }
  },
  zhifusub:function(e){
    // var that = this;
    var couponId = this.data.couponId;
    var pay = this.data.number;
    // if(!pay){
    //   return app.Tips({title:"请填写付款金额"});
    // }
    // 提交确认提示
    this.verifyForm(pay,couponId);

  },
  //确认支付
  comfirmPay:function(pay, couponId, point) {
    
    zhifu(pay,couponId, '' ).then(function (res) {
      // app.Tips({ title: '支付成功' });
      let contentText = '+'+point+'积分';
      if (res.status === 200) {
        
        wx.showModal({
          title: '支付成功',
          content: contentText,
          showCancel: false,
          confirmText: '返回首页',
          success(res){
            if (res.confirm){
              wx.switchTab({url:'/pages/index/index'});
            } else if (res.cancel){
              return app.Tips({title:'已取消'});
            }
          },
        })

      } else {
        app.Tips({ title: res.msg });
      }

    },function(res){
      return app.Tips({title:res.msg});
    });
  },
  openSubscribe:function(e){
    let page = e.currentTarget.dataset.url;
    wx.showLoading({
      title: '正在加载',
    })
    openRechargeSubscribe().then(res => {
      wx.hideLoading();
      wx.navigateTo({
        url: page,
      });
    }).catch(() => {
      wx.hideLoading();
    });
  },
  getCouponList:function(){
    var that=this;
    let data = { cartId: 0}
    getCouponsOrderPrice(0, data).then(res=>{
      that.setData({ 'coupon.list': res.data, openType: 1 });
    });
  },
  /**
   * 获取用户详情
  */
  getUserInfo:function(){
    let that = this;
    getUserInfo().then(res=>{
      that.setData({ 
        userInfo: res.data, 
        recharge_switch: res.data.recharge_switch
      });
    });
  },
  /**
   * 获取活动可参与否
  */
  get_activity:function(){
    var that=this;
    userActivity().then(res=>{
      that.setData({ activity: res.data });
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isLog && this.data.isClose) {
      this.getUserInfo();
      this.get_activity();
    }
  },
  coupon: function () {
    this.setData({
      'coupon.coupon': true
    })
  },
  ChangCouponsClone:function(){
    this.setData({'coupon.coupon':false});
  },
    /**
   * 处理点击优惠券后的事件
   * 
  */
  ChangCoupons:function(e){
    var index = e.detail, list = this.data.coupon.list, couponTitle = '请选择', couponId = 0;
    for (var i = 0, len = list.length; i < len; i++) {
      if(i != index){
        list[i].use_title = '';
        list[i].is_use = 0;
      }
    }
    if (list[index].is_use) {
      //不使用优惠券
      list[index].use_title = '';
      list[index].is_use = 0;
    } else {
      //使用优惠券
      list[index].use_title = '不使用';
      list[index].is_use = 1;
      couponTitle = list[index].coupon_title;
      couponId = list[index].id;
    }
    this.setData({ 
      couponTitle: couponTitle, 
      couponId: couponId, 
      'coupon.coupon': false, 
      "coupon.list":list,
    });
    // this.computedPrice();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ isClose: true });
  },
})